package com.prs.crm.dao.oa.impl;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.MailItemDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MailBox;
import com.prs.crm.domain.oa.MailItem;

public class MailItemDaoImpl extends BaseDaoImpl<MailItem> implements MailItemDao{

	 
	public boolean exist(final MailBox box,final String messageId,final Person reciver)
	{
		return (Boolean)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
						
					        	Criteria criteria = session.createCriteria(MailItem.class);
					        	criteria.add(Restrictions.eq("messageId", messageId));
					        	criteria.add(Restrictions.eq("mailBox", box));
					        	criteria.add(Restrictions.eq("reciver", reciver));
					        	criteria.setProjection(Projections.rowCount());
					        	int count = (Integer)criteria.uniqueResult();
					        	if (count>0)
					        		return true;
					        	else
					        		return false;
				        }   
				 } );
	}
}
